﻿using UnityEngine;
using System;
using System.Collections;
using FairyGUI;
using FairyGUI.Utils;
using DG.Tweening;
using Project;
using XLua;

namespace Project.UI
{
    [LuaCallCSharp]
    public class ViewController : GComponent
    {

        public override void ConstructFromXML(FairyGUI.Utils.XML xml)
        {

            base.ConstructFromXML(xml);
            this.ViewDidLoad(xml);
            
        }

        protected override void OnUpdate()
        {
            base.OnUpdate();
            ViewUpdate();
        }


        protected virtual void ViewOnStage() { }
        protected virtual void ViewWillLoad() { }

        public virtual void ViewDidLoad(FairyGUI.Utils.XML xml) { }

        protected virtual void ViewAnimeInComplete() { }

        protected virtual void ViewAnimeOutComplete() { }
        protected virtual void Dispose() { }
        protected virtual void ViewUnLoad() { }

        protected virtual void ViewUpdate(){ }

        protected virtual void ViewReLoad() { }
        public void Reload()
        {
            ViewReLoad();
        }


        public virtual void PresentViewController(ViewController viewController, bool animated, Action action)
        {
            if (animated)
            {
                Animate(viewController, true, () =>
                    {
                        if (action != null) action();
                    }
                    );
            }
            else
            {
                if (action != null) action();
            }
        }


        public virtual void DismissViewController(bool animated, Action action, bool destroy)
        {
            if (animated)
            {
                Animate(this, false, () =>
                    {
                        if (action != null) action();
                        if (destroy)
                        {
                            UIManager.Instance.RemoveView(this);
                        }
                    }
                    );
            }
            else
            {
                if (action != null) action();
                if (destroy)
                {
                    UIManager.Instance.RemoveView(this);  
                }
            }
        }

        public void Animate(ViewController viewController, bool forward, Action callback)
        {
            Tween tween = null;
            if (forward)
            {
                viewController.x = GRoot.inst.width;
                tween = viewController.TweenMove(new Vector2(0, 0), 0.35f).SetEase(Ease.OutCirc);
                tween.OnComplete(
                    delegate()
                    {
                        viewController.ViewAnimeInComplete();
                        if (callback != null) callback();
                    }
                    );
            }
            else
            {
                tween = viewController.TweenMove(new Vector2(GRoot.inst.width, 0), 0.35f).SetEase(Ease.OutCirc);
                tween.OnComplete(
                    delegate()
                    {
                        viewController.ViewAnimeInComplete();
                        if (callback != null) callback();
                    }
                    );
            }
        }


       


    }
}

